<template>
    <div style="width: 100%; height: 100%; border: green 1px solid">
				<div style="display: flex;justify-content: space-between;">
					<h2 style="text-align: center;">修改/添加</h2>
					<h2><button @click="go2show()">返回原本位置</button></h2>
				
				</div>
	            <hr>
	            编号：<input type="text" v-model="stu.uid" readonly="readonly"/><br>
	            名字：<input type="text" v-model="stu.uname"/><br>
	            账号：<input type="text" v-model="stu.phone"><br>
	            密码：<input type="text" v-model="stu.pwd"><br>
	            教室：<select v-model="stu.clz.clzno" >
	            <option v-for="x,idx in clzs" v-bind:value="x.clzno">{{x.clzname}}</option>
	        </select><br>
	            图片：<input type="file" ref="img" /><br>
	            <div style="width: 10vw;height: 10vw">
	                <img v-bind:src="$ip+`/api/public/showimg/`+stu.pic" style="width: 100% ;height: 100%">
	            </div>
	            <button @click="addup()">添加/修改</button>
				{{doadd}}
	        </div>
			
</template>


<script>
	export default{
		data(){
			return{
				//定义需要修改添加的学生函数
				stu:{uid:0,clz:{clzno:0}},
				clzs:[],
				token:"",
				doadd:''
			}
		},
		methods:{
			addup(){
				var that=this
				 //判断输入数据是否为空
				if(that.stu.uname==undefined||that.stu.phone==undefined||
					that.stu.pwd==undefined||that.stu.clz.clzno==undefined){
				    alert("添加不允许为空")
				    return;
				}
				let path=""
				if(that.stu.uid==0)path=this.$ip+"/api/adm/stu/add"
				if(that.stu.uid>0)path=this.$ip+"/api/adm/stu/update"
				console.log(path)
				var fd=new FormData()
				fd.append("uid",that.stu.uid)
				fd.append("uname",that.stu.uname)
				fd.append("phone",that.stu.phone)
				fd.append("pwd",that.stu.pwd)
				fd.append("clz.clzno",that.stu.clz.clzno)
				var filex=that.$refs.img.files[0]
				fd.append("mypic",filex)
					console.log(fd)
				this.$axios.post(path,fd,{headers:{'Authorization':that.token}}).then(function(resp){
					that.doadd=resp.data.msg
					console.log(resp.data)
				})
			},
			go2show(){
				this.$router.push("/adm/stushow")				 
								 
			}
		},
		mounted() {
			this.token=window.localStorage.getItem("token")
			var a=this.$route.query.id
			console.log(a)
			var that=this
			if(a>0){
			
			this.$axios.get(this.$ip+"/api/adm/stu/findById/"+a,{headers:{'Authorization':that.token}}).then(function(resp){
				 	that.stu=resp.data.data
				 	console.log(resp.data.data)
				})
				}
			this.$axios.get(this.$ip+"/api/public/getClzs").then(function(resp){
				that.clzs=resp.data.data
				console.log(resp.data.data)
			})
			if(a==0) this.stu={uid:0,clz:{clzno:0}}
			
		}
	}
</script>

<style>
</style>
